<!--
  -    Copyright (c) 2018-2025, cloud All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: cloud
  -->
<template>
  <BasicContainer>
    <FyCurd
      ref="crud"
      v-model="form"
      :page.sync="page"
      :data="tableData"
      :permission="permissionList"
      :table-loading="tableLoading"
      :option="tableOption"
      :before-open="handleOpenBefore"
      right-search
      @selection-change="selectionChange"
      @on-load="getList"
      @search-change="searchChange"
      @refresh-change="refreshChange"
      @size-change="sizeChange"
      @current-change="currentChange"
      @row-update="handleUpdate"
      @row-save="handleSave"
      @row-del="handleDel">
      <template #menuLeft>
        <ElButton
          v-if="permissions.purchaseApply_fyassetpurchaseapply_export"
          class="filter-item"
          plain
          type="primary"
          size="small"
          icon="el-icon-download"
          @click="exportExcel">
          导出
        </ElButton>
      </template>
      <!-- 采购清单子表单-->
      <template #infoForm>
        <PurchaseApplyPage
          ref="PurchaseApplyPage"
          :batch-number="batchNumber"
          @openSau="openSau"
          @openPurchaseApply="openPurchaseApply" />
      </template>
      <!-- 附件上传组件{ 需要传入uniqueCode/model/classify} -->
      <template #uploadPageForm>
        <UploadPage ref="UploadPage" :unique-code="uniqueCode" model="asset" classify="budget" />
      </template>

      <!-- 历史记录选择  -->
      <!-- 选择按钮 -->
      <template #selectBtn>
        <ElButton type="primary" plain @click="checkData">选择</ElButton>
      </template>
      <!-- 行展开显示子表格数据-->
      <template #expand="{ row }">
        <PurchaseApplyPage ref="PurchaseApplyExpand" :view-type="viewType" :batch-number="row.batchnumber" />
      </template>
    </FyCurd>
    <!--
        在子单中弹出标准资产型号管理模态框
        TODO 解决遮罩层问题
      -->
    <ElementDialog title="选择物品" :visible.sync="assetInfo" width="90%">
      <Sau ref="sau" />
      <template #footer>
        <ElButton @click="assetInfo = false">取 消</ElButton>
        <ElButton type="primary" @click="checkPurchaseApply">确 定</ElButton>
      </template>
    </ElementDialog>
    <!-- 弹出标主单模态框  自己打开自己 -->
    <ElementDialog
      title="选择采购申请"
      :visible.sync="purchaseApplyVisible"
      :before-close="dialogPurApplyClose"
      width="90%">
      <Fyassetpurchaseapply ref="purchaseApply" :expand="true" />
      <template #footer>
        <ElButton @click="cancelPurApplyDialog">取 消</ElButton>
      </template>
    </ElementDialog>
  </BasicContainer>
</template>

<script>
  import { addChildList, addObj, delObj, fetchList, getBatchNumber, putObj } from '@/api/asset/fyassetpurchaseapply'
  import { selectOption, tableOption } from '@/const/crud/asset/fyassetpurchaseapply'
  import { getUUID } from '@/api/admin/uuid'

  import PurchaseApplyPage from '@/module/asset/purchaseApplyList/index'
  import Sau from '@/module/asset/sau/sau'
  import { services } from '@/config'
  import { mapState } from 'vuex'

  export default {
    name: 'Fyassetpurchaseapply',
    components: {
      PurchaseApplyPage,
      Sau
    },
    props: {
      // 子表单选择时打开行 记录
      expand: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        searchForm: {},
        tableData: [],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20 // 每页显示多少条
        },
        tableLoading: false,
        tableOption,
        uniqueCode: '',
        form: {},
        batchNumber: '', // 批号
        viewType: '', // 视图当前状态
        assetInfo: false, // 资产信息 默认为false
        purchaseApplyVisible: false // 主单模态框显示 默认为false
      }
    },
    computed: {
      ...mapState('user', ['permissions']),
      permissionList() {
        return {
          addBtn: this.$isEmpty(this.permissions.purchaseApply_fyassetpurchaseapply_add, false),
          delBtn: this.$isEmpty(this.permissions.purchaseApply_fyassetpurchaseapply_del, false),
          editBtn: this.$isEmpty(this.permissions.purchaseApply_fyassetpurchaseapply_edit, false)
        }
      }
    },
    created() {
      this.expandFunc()
    },
    methods: {
      // 选择时使用
      expandFunc() {
        if (this.expand) {
          this.tableOption = selectOption
          this.viewType = 'check'
        }
      },
      // 列表查询
      getList(page, params) {
        this.tableLoading = true
        fetchList(
          Object.assign(
            {
              current: page.currentPage,
              size: page.pageSize
            },
            params,
            this.searchForm
          )
        )
          .then(response => {
            this.tableData = response.data.data.records
            this.page.total = response.data.data.total
            this.tableLoading = false
          })
          .catch(() => {
            this.tableLoading = false
          })
      },
      // 删除
      handleDel: function (row, index) {
        this.$confirm(`是否确认删除ID为${row.id}`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            return delObj(row.id)
          })
          .then(data => {
            this.$message.success('删除成功')
            this.getList(this.page)
          })
      },
      // 更新
      handleUpdate: function (row, index, done, loading) {
        putObj(row)
          .then(data => {
            // 获取子清单的数据
            const childList = this.$refs.PurchaseApplyPage.tableData
            if (childList.length !== 0) {
              addChildList(childList)
            }
          })
          .then(data => {
            this.$message.success('修改成功')
            done()
            this.getList(this.page)
          })
          .catch(() => {
            loading()
          })
      },
      // 保存
      handleSave: function (row, done, loading) {
        addObj(row)
          .then(data => {
            // 获取子清单的数据
            const childList = this.$refs.PurchaseApplyPage.tableData
            if (childList.length !== 0) {
              addChildList(childList)
            }
          })
          .then(data => {
            this.$message.success('添加成功')
            done()
            this.getList(this.page)
          })
          .catch(() => {
            loading()
          })
      },
      // 每页条数改变事件
      sizeChange(pageSize) {
        this.page.pageSize = pageSize
      },
      // 当前页发生改变事件
      currentChange(current) {
        this.page.currentPage = current
      },
      // 查询事件
      searchChange(form, done) {
        this.searchForm = form
        this.page.currentPage = 1
        this.getList(this.page, form)
        done()
      },
      // 刷新事件
      refreshChange() {
        this.getList(this.page)
      },
      //  导出excel
      exportExcel() {
        this.$downBlobFile(
          `${services.assetService}fyassetpurchaseapply/export`,
          this.searchForm,
          'fyassetpurchaseapply.xlsx'
        )
      },
      handleOpenBefore(show, type) {
        // 添加附件参数
        if (type === 'edit') {
          this.uniqueCode = this.form.unid
          this.batchNumber = this.form.batchnumber
        } else {
          getUUID().then(response => {
            this.uniqueCode = response.data.data
            this.form.unid = response.data.data
          })
          getBatchNumber().then(response => {
            this.batchNumber = response.data.data
            this.form.batchnumber = response.data.data
          })
        }
        window.boxType = type
        show()
      },
      selectionChange(list) {
        // this.$message.success('选中的数据'+ JSON.stringify(list));
      },
      checkPurchaseApply() {
        this.$refs.PurchaseApplyPage.tableData = this.$refs.sau.resultData
        for (let i = 0; i < this.$refs.PurchaseApplyPage.tableData.length; i++) {
          this.$refs.PurchaseApplyPage.tableData[i].batchnumber = this.batchNumber
        }
        this.$message.success(`选中的数据${JSON.stringify(this.$refs.PurchaseApplyPage.tableData)}`)
        this.assetInfo = false
      },
      openSau() {
        this.$message.success('被触发')
        this.assetInfo = true
      },
      openPurchaseApply() {
        this.$message.success('被触发')
        this.purchaseApplyVisible = true
      },
      // 点击 x 关闭历史单据选择模态框
      dialogPurApplyClose(done) {
        this.$refs.PurchaseApplyPage.init()
        done()
      },
      // 点击取消按钮关闭历史单据选择模态框
      cancelPurApplyDialog() {
        this.$refs.PurchaseApplyPage.init()
        this.purchaseApplyVisible = false
      },
      checkData() {
        this.$message.success(`选中的数据${JSON.stringify(this.$refs.PurchaseApplyExpand.tableData)}`)
        this.purchaseApplyVisible = false
      }
    }
  }
</script>
